/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.file.fdo;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Properties;

import org.vnetcon.blobdb.database.backend.file.FilePersistenceManager;
import org.vnetcon.blobdb.database.backend.file.FileTransaction;

public class PMF {

	// searched by table id
	public static Hashtable<Integer, TableData> hTableData = new Hashtable<Integer, TableData>();
	// searched by table name + schema
	public static Hashtable<Integer, Table> hTable = new Hashtable<Integer, Table>();
	// searched without parameters
	public static Hashtable<Integer, TableID> hTableID = new Hashtable<Integer, TableID>();

	private static String datadir = null;
	
	public static FilePersistenceManager getPersistenceManager(){
		FileTransaction trans = new FileTransaction();
		FilePersistenceManager mempm = new FilePersistenceManager(trans);
		initTables();
		return mempm;
	}
	
	private static void initTables(){
		InputStream fIn = PMF.class.getResourceAsStream("/blobdb/blobdb.properties");
		BufferedReader bfIn = new BufferedReader(new InputStreamReader(fIn));
		Properties p = new Properties();
		try{
			p.load(bfIn);
			datadir = p.getProperty("database.dir", null);
			bfIn.close();
			
			File f = new File(datadir);
			String[] files = f.list();
			int i = 0;
			while(i < files.length){
				String file = files[i];

				if(file.startsWith("TableCols_")){
					file = file.substring(file.indexOf("_") + 1, file.indexOf("."));
					int tableID = Integer.parseInt(file);
					Table t = new Table(tableID, null, null, -1, -1);
					hTable.put(tableID, t);
				}
				
				if(file.startsWith("TableDataProps_")){
					file = file.substring(file.indexOf("_") + 1);
					String tableId = file.substring(0, file.indexOf("_"));
					file = file.substring(file.indexOf("_") + 1, file.indexOf("."));
					int tableID = Integer.parseInt(file);
					int tableDataID = Integer.parseInt(tableId);
					TableData t = new TableData(tableID, tableDataID);
					hTableData.put(tableID, t);
				}
				
				i++;
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}
	
}
